$(function () {
    obj.init();
});
var obj = new function () {
   'use strict';
   return {
       form:null,
       targetDom:null,
       teachers:null,
       init:function () {
           this.form = new xhForm($('#xh_form'));
           this.form.parse();
           $('[name=iSchoolId],[name=iBPPId]').select2();//学校选择
           this.listener();
       },
       listener:function () {
           var $this = this;

           //类型选择
           $('[name=iType]').on('change',function () {
               $this.funObj().bindTypeSelected($(this).find('option:selected').text());
           });

           //学校选择
           $('[name=iSchoolId]').on('change',function () {
               $this.funObj().bindSchoolSelected($(this).val());
           });

           //教师选择
           $('#xh_form').on('change','[name="sTeachers[]"]',function () {
               $this.funObj().tchSelected($(this).val());
           });

           $('#xh_form').on('change','[name=iCUId]',function () {
               $this.funObj().custSelected($(this).val());
           });

           //form提交
           $('#bt_modal_confirm',parent.document).on('click',function () {
               $this.funObj().formSubmit();
           })
       },
       funObj:function () {
           var $this = this;
            return {
                bindTypeSelected:function (text) {
                    var numDom = $('[name=iTchNum]');
                    if($('#demoDom').length > 0){
                        $('#demoDom').remove();
                    }
                    if(text === '教师机'){
                        numDom.attr('readonly','readonly').val(0);
                        $this.targetDom = $('<tr>\n' +
                            '                <td><label>教师</label></td>\n' +
                            '                <td colspan="2">\n' +
                            '                    <select name="sTeachers[]" multiple  class="form-control">\n' +
                            '                    </select>\n' +
                            '                </td>\n' +
                            '                <td>\n' +
                            '                   <a id="allSelect" class="btn btn-primary" >全部选中</a>'+
                            '                </td>\n' +
                            '            </tr>');
                        numDom.parent().parent().before($this.targetDom);
                        $('[name="sTeachers[]"]').select2();
                        $('#allSelect').unbind('click').bind('click',function () {
                            $this.funObj().selectAll();
                        })
                    }else{
                        if($this.targetDom !=null){
                            $this.targetDom.remove();
                            $this.targetDom =null;
                        }
                        numDom.removeAttr('readonly').val(0);
                    }
                    if (text == '自用机'){
                        $('[name="iType"]').parent().next().hide();
                        $('[name="iType"]').parent().next().next().hide();

                        var selfUseStr = '<tr id="demoDom">\n' +
                            '                <td style="width: 75px;"><label>演示机</label></td>\n' +
                            '                <td style="width: 200px">\n' +
                            '                    <select name="bDemo" class="form-control">\n' +
                            '                        <option value="0" selected>否</option>\n' +
                            '                        <option value="1">是</option>\n' +
                            '                    </select>\n' +
                            '                </td>\n' +
                            '                <td style="width: 75px;"><label>客户</label></td>\n' +
                            '                <td>\n' +
                            '                    <select name="iCUId" class="form-control" style="width: 170px">\n' +
                            '                        <option value="" selected >请选择</option>\n' ;
                                        $.each(cusInfo, function (id,text) {
                                            selfUseStr += '<option value="'+id+'">'+text+'</option>';
                                        });
                        selfUseStr+='                    </select>\n' +
                            '                </td>\n' +
                            '            </tr>';

                        $('[name="iBPPId"]').parent().parent().before(selfUseStr);
                        $('[name=iCUId]').select2();
                    }else {
                        $('[name="iType"]').parent().next().show();
                        $('[name="iType"]').parent().next().next().show();
                    }
                },
                bindSchoolSelected:function (schoolId) {
                    xhExtra.ajaxRequest('other/getTchBySchoolId',{schoolId:schoolId},{},function (data) {
                        var dom = $('[name="sTeachers[]"]');
                        var str = '';
                        $this.teachers = data;
                        $.each(data, function (id, text) {
                            str += '<option value="' + id + '">' + text + '</option>';
                        });
                        dom.empty().append(str);
                    });
                    xhExtra.ajaxRequest('device/ajaxGetPlanName',{schoolId:schoolId},{},function (res) {
                        var html = '<option value="0">请选择</option>';
                        if (res.length > 0) {
                            $.each(res, function (index, item) {
                                html += '      <option value="' + item['id'] + '" ';
                                html += '>' + item['name'] + '</option>\n';
                            });
                        }
                        $('[name=iBPPId]').empty().append(html);
                    });
                },
                custSelected:function (cuId) {
                    xhExtra.ajaxRequest('device/ajaxGetPlanName',{cuId:cuId},{},function (res) {
                        var html = '<option value="0">请选择</option>';
                        if (res.length > 0) {
                            $.each(res, function (index, item) {
                                html += '      <option value="' + item['id'] + '" ';
                                html += '>' + item['name'] + '</option>\n';
                            });
                        }
                        $('[name=iBPPId]').empty().append(html);
                    });
                },
                tchSelected:function (val) {
                    $('[name=iTchNum]').val(val.length);
                },
                formSubmit:function () {
                    $this.form.submit('device/saveOtherApply',{},function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                        window.top.xhTop.closeModal();
                    })
                },
                selectAll:function () {
                    var arr = [];
                    $.each($this.teachers,function (id,text) {
                        arr.push(id);
                    });
                    $('[name="sTeachers[]"]').val(arr).trigger('change');
                }
            }
       }
   }
};